package com.itheima.task;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.Set;

@Component
public class RedisImgTask {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;

    @Scheduled(cron = "0 0 1 * * ?")
    public void deleteImgTask(){
        System.out.println(LocalDate.now());
        SetOperations setOperations = redisTemplate.opsForSet();
        Set difference = setOperations.difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        for (Object o : difference) {
            String s = ((String)o);
            s = s.substring(s.lastIndexOf("/")+1, s.length());
            aliOssUtil.delete(s);
            setOperations.remove(RedisConstant.SETMEAL_PIC_RESOURCES, o);
        }
    }
}
